package org.jash.shop;

import android.content.Intent;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.jash.shop.entity.GoodsEntity;

import java.util.HashMap;

public class ShopActivity extends AppCompatActivity {
    private GridView gv;
    private GoodsAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        gv = findViewById(R.id.gv);
        adapter = new GoodsAdapter();
        gv.setAdapter(adapter);
        HttpUtils.doGet("http://10.161.9.80:7003/goods/getAllGoods", new HashMap<String, Integer>() {{
            put("goods_category", 0);
        }}, GoodsEntity.class,
                r -> {
                    if (r.getErrorCode() == 200) {
                        adapter.addAll(r.getData());
                    } else {
                        Toast.makeText(this, r.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                },
                t -> {
                    Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                });
        findViewById(R.id.nav_3).setOnClickListener(v -> startActivity(new Intent(this, CartActivity.class)));
        gv.setOnItemClickListener((parent, view, position, id) -> {
            startActivity(new Intent(this, DetailActivity.class).putExtra("goods", adapter.getItem(position)));
        });
    }
}